package cn.szzsi.business.interceptor;


import cn.szzsi.model.ActionRole;
import cn.szzsi.model.Member;
import com.jfinal.aop.Interceptor;
import com.jfinal.aop.Invocation;
import com.jfinal.core.Controller;
import com.jfinal.kit.StrKit;

import java.util.List;

/**
 * 角色动作
 * Created by zhengy on 2017/3/20.
 */
public class RoleActionInterceptor implements Interceptor {
    public void intercept(Invocation ai) {
        Controller c = ai.getController();
        String loginMemberId = c.getAttr("userId");//获取当前登录用户
        if(StrKit.notBlank(loginMemberId) && (c.getSessionAttr("actionIds")==null || "".equals(c.getSessionAttr("actionIds")))){
            Member member=Member.dao.findById(loginMemberId);
            String b_shoprank=member.getStr("b_shoprank");
            c.setAttr("b_shoprank",b_shoprank);
            String actionids = "";//动作字符串
            String roleId=member.getStr("roleId");//根据用户获取角色
            if(StrKit.notBlank(roleId)){
                List<ActionRole> larr= ActionRole.dao.getByRoleId(roleId);//根据角色获取该角色绑定哪些动作
                for(ActionRole arr:larr){
                    actionids = actionids + "," + arr.getStr("actionId");
                }
            }
            c.setSessionAttr("actionIds",actionids);
        }

        if(c.getSessionAttr("actionIds")!=null)
            c.setAttr("actionIds",c.getSessionAttr("actionIds"));

        ai.invoke();
    }
}
